import { fileURLToPath } from 'url';
import {resolve, dirname} from 'path';
import {readdirSync, readFileSync, writeFileSync} from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const iconDir = resolve(__dirname, '../src/assets/icons');
const iconfontFile = resolve(__dirname, '../public/icons.js');
// 获取图标列表
const list = readdirSync(iconDir);
let svgContent = 'window._iconfont_svg_string_4910636=\'<svg aria-hidden="true" style="position: absolute; width: 0px; height: 0px; overflow: hidden;">';
list.forEach(filename => {
    const symbolId = filename.replace(/icon_tool_tab_(.*?)\.svg/, '$1').replace(/\.svg/, '');
    const filePath = resolve(iconDir, filename);
    const iconContent = readFileSync(filePath, 'utf8');
    const viewBoxMatch = iconContent.match(/viewBox="(.*?)"/);
    const viewBox = viewBoxMatch && viewBoxMatch[1] || '0 0 20 20';
    svgContent += `<symbol id="icon-${symbolId}" viewBox="${viewBox}">`;
    svgContent += iconContent
        .replace(/<svg[^>]*>(.*?)<\/svg>/, '$1')
        .replace(/<path[^>]*>(.*?)<\/path>/g, (match) => {
            return match.replace(/stroke\="(.*?)"/g, 'stroke="currentColor"')
                .replace(/fill\="(.*?)"/g, 'fill="currentColor"')
        })
    svgContent += '</symbol>'
})
svgContent += '</svg>\';'
svgContent += '(l=>{var a=(h=(h=document.getElementsByTagName("script"))[h.length-1]).getAttribute("data-injectcss"),h=h.getAttribute("data-disable-injectsvg");if(!h){var c,i,v,t,o,e=function(a,h){h.parentNode.insertBefore(a,h)};if(a&&!l.__iconfont__svg__cssinject__){l.__iconfont__svg__cssinject__=!0;try{document.write("<style>.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}</style>")}catch(a){console&&console.log(a)}}c=function(){var a,h=document.createElement("div");h.innerHTML=l._iconfont_svg_string_4910636,(h=h.getElementsByTagName("svg")[0])&&(h.setAttribute("aria-hidden","true"),h.style.position="absolute",h.style.width=0,h.style.height=0,h.style.overflow="hidden",h=h,(a=document.body).firstChild?e(h,a.firstChild):a.appendChild(h))},document.addEventListener?~["complete","loaded","interactive"].indexOf(document.readyState)?setTimeout(c,0):(i=function(){document.removeEventListener("DOMContentLoaded",i,!1),c()},document.addEventListener("DOMContentLoaded",i,!1)):document.attachEvent&&(v=c,t=l.document,o=!1,d(),t.onreadystatechange=function(){"complete"==t.readyState&&(t.onreadystatechange=null,m())})}function m(){o||(o=!0,v())}function d(){try{t.documentElement.doScroll("left")}catch(a){return void setTimeout(d,50)}m()}})(window);';
writeFileSync(iconfontFile, svgContent, 'utf8');